package com.ruoyi.wifiauth.service.impl;

import com.ruoyi.wifiauth.service.IGuidePageAccessService;
import com.ruoyi.wifiauth.mapper.GuidePageAccessMapper;
import com.ruoyi.wifiauth.domain.GuidePageAccess;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Date;
import java.util.UUID;

/**
 * 引导页访问记录Service业务层处理
 */
@Service
public class GuidePageAccessServiceImpl implements IGuidePageAccessService {

    @Autowired
    private GuidePageAccessMapper guidePageAccessMapper;

    @Override
    public int saveGuidePageAccess(GuidePageAccess guidePageAccess) {
        // 设置默认值
        if (guidePageAccess.getCreateTime() == null) {
            guidePageAccess.setCreateTime(new Date());
        }
        if (guidePageAccess.getStatus() == null) {
            guidePageAccess.setStatus(1); // 默认有效状态
        }
        // 如果没有sessionId，生成一个
        if (guidePageAccess.getSessionId() == null || guidePageAccess.getSessionId().isEmpty()) {
            guidePageAccess.setSessionId(generateSessionId());
        }
        
        return guidePageAccessMapper.insert(guidePageAccess);
    }

    @Override
    public GuidePageAccess selectById(Long id) {
        return guidePageAccessMapper.selectById(id);
    }

    @Override
    public GuidePageAccess selectBySessionId(String sessionId) {
        return guidePageAccessMapper.selectBySessionId(sessionId);
    }

    @Override
    public List<GuidePageAccess> selectByDeviceId(String deviceId) {
        return guidePageAccessMapper.selectByDeviceId(deviceId);
    }

    @Override
    public List<GuidePageAccess> selectGuidePageAccessList(GuidePageAccess guidePageAccess) {
        return guidePageAccessMapper.selectGuidePageAccessList(guidePageAccess);
    }

    @Override
    public int updateGuidePageAccess(GuidePageAccess guidePageAccess) {
        return guidePageAccessMapper.update(guidePageAccess);
    }

    @Override
    public int deleteById(Long id) {
        return guidePageAccessMapper.deleteById(id);
    }

    /**
     * 生成会话ID
     */
    private String generateSessionId() {
        return "GP_" + System.currentTimeMillis() + "_" + UUID.randomUUID().toString().replace("-", "").substring(0, 8);
    }
} 